package com.xci.sys.api;

import com.xci.core.base.RestMessage;
import com.xci.core.base.PageList;
import com.xci.core.domain.PageModel;
import com.xci.core.annotation.Authorize;
import com.xci.core.base.ApiController;
import com.xci.core.internal.Const;
import com.xci.sys.entity.SysFile;
import com.xci.sys.service.FileService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件接口
 * @author 吕艳阳
 */
@ApiSort(12)
@Api(tags = "文件接口")
@Authorize
@RestController
@RequestMapping(value = "/api/sys/file", produces = Const.P_JSON)
public class FileApiController extends ApiController {
    @Resource
    private FileService fileService;//文件服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建文件")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysFile entity) {
        return fileService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "删除文件")
    @ApiImplicitParam(name = "id", value = "文件主键", required = true)
    @PostMapping("/delete")
    public RestMessage delete(String id) {
        return fileService.delete(id);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "根据记录主键删除文件")
    @ApiImplicitParam(name = "recordId", value = "记录主键", required = true)
    @PostMapping("/deleteByRecordId/{recordId}")
    public RestMessage deleteByRecordId(@PathVariable String recordId) {
        return fileService.delete(recordId);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "根据记录主键检查文件是否存在")
    @ApiImplicitParam(name = "recordId", value = "记录主键", required = true)
    @PostMapping("/existByRecordId/{recordId}")
    public RestMessage existByRecordId(@PathVariable String recordId) {
        return RestMessage.result(fileService.existByRecordId(recordId));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "查询单个文件")
    @ApiImplicitParam(name = "id", value = "文件主键", required = true)
    @PostMapping("/selectById/{id}")
    public RestMessage<SysFile> selectById(@PathVariable String id) {
        return RestMessage.success(fileService.selectById(id));
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "根据记录主键获取第一个文件对象")
    @ApiImplicitParam(name = "recordId", value = "记录主键", required = true)
    @PostMapping("/selectByRecordId/{recordId}")
    public RestMessage<SysFile> selectByRecordId(@PathVariable String recordId) {
        return RestMessage.success(fileService.selectByRecordId(recordId));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "根据记录主键获取文件对象分页列表")
    @ApiImplicitParam(name = "recordId", value = "记录主键", required = true)
    @PostMapping("/selectPageList")
    public RestMessage<PageList<SysFile>> selectPageList(String recordId,PageModel p) {
        return RestMessage.success(PageList.of(fileService.selectPageListByRecordId(recordId)));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "根据记录主键获取文件对象列表")
    @ApiImplicitParam(name = "recordId", value = "记录主键", required = true)
    @PostMapping("/selectListByRecordId/{recordId}")
    public RestMessage<List<SysFile>> selectListByRecordId(@PathVariable String recordId) {
        return RestMessage.success(fileService.selectListByRecordId(recordId));
    }
}